iT邦幫忙

2021 iThome 鐵人賽

DAY 5
0
自我挑戰組

Powershell 入门系列 第 5

Powershell 远程连接

  • 分享至 

  • xImage
  •  

使用过 Linux 的都知道,Linux 的 shell 不但可以本地运行,也可以通过远程的方式连接。Powershell 也是如此,不但可以在本地运行,也可以通过远程的方式运行,不过它使用的协议,和 Linux 的 shell 不太一样。

Powershell 使用 WinRM 进行远程连接的,所以你想要通过 Powershell 远程连接到远程主机,必须先要在远程主机上配置 WinRM。

下面我们来看一下,怎么配置吧:
在远程主机上开启远程连接:

PS C:\Users\Administrator> Enable-PSRemoting

对于多主机的情况下,你可以通过 Windows AD 的组策略进行配置。

在本地,设置远端主机是信任的:

PS C:\Windows\system32> Set-Item WSMan:\localhost\Client\TrustedHosts -Value "192.168.1.12"

WinRM Security Configuration.
This command modifies the TrustedHosts list for the WinRM client. The computers in the TrustedHosts list might not be
authenticated. The client might send credential information to these computers. Are you sure that you want to modify
this list?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y

建立远程连接:

PS C:\Windows\system32> Enter-PSSession -ComputerName 192.168.1.12 -Credential Administrator

在弹出的验证窗口中,输入密码:
https://ithelp.ithome.com.tw/upload/images/20210918/20099494YQCgCo6Z3H.png
(通过 Windows AD 验证,是一个比较好的选项。)

建立起连接后,你就可以像在本地一样,运行远端主机的 powershell 了:

[192.168.1.12]: PS C:\Users\Administrator\Documents>

在 powershell 中,还有一个一对多建立远程的命令 —— Invoke-Command,有兴趣的可以自己试一下,使用方式类似,只是后面计算机名是多个主机名,或 IP,之间使用逗号隔开。


上一篇
Powershell 入门之命令的输出
下一篇
Powershell 入门之基本运算符
系列文
Powershell 入门21
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言